/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package medie;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.*;
import java.util.*;
import java.sql.*;
/**
 * Classe che calcola la media dei voti data ad un utente
 * @author francescoparis
 */
public class Medie {

    /**
     * Metodo main che calcola la media data ad un utente
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        //scrivi su file i dati delle medie
        try{
        FileOutputStream prova = new FileOutputStream("medie.dat");
        PrintStream scrivi = new PrintStream(prova);
        
        Utilita.loadProperties();
        Utilita.loadDriver();
        Utilita.doConnection();
        for(int i=2795; i<= 220970; i++){
            int n = 0;
            int s = 0;
            ResultSet res1 = Utilita.contaUtente(i);
            while(res1.next()){
                n = res1.getInt(1);
            }
            ResultSet res2 = Utilita.sommaUtente(i);
            while(res2.next()){
                s = res2.getInt(1);
            }
            if(n == 0){
                scrivi.println(i+",1");
                System.out.println(i+",1");
            }else{
                int m = (int) s/n;
                scrivi.println(i+","+m);
                System.out.println(i+","+m);
            }
        }/*fine for*/
        
        
        
        scrivi.close();
        prova.close();
        
        Utilita.closeConnection();
        }catch(IOException exception){
            System.out.println("Errore processamento file" + exception);
        }
        catch(SQLException ex){
                ex.printStackTrace();
            }/*fine catch*/
        
        
    }/*fine main*/
    
    
    
    
    
    
    
}
